package concurrentV;

public class TwoThreadPrint10_abcd1234 {
    private static boolean isLetter = true;
    private static int index = 1;
    private  static Object lock = new Object();

    private void printLetter(){
        for (int i = 0; i < 40; i++) {
            synchronized (lock){
                if(!isLetter){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                char c = (char) ('a' + index -1);
                System.out.println(Thread.currentThread().getName()+":"+c);
                //index = index % 4 + 1;
                isLetter = !isLetter;
                lock.notifyAll();
            }
        }
    }

    private void printNum(){
        for (int i = 0; i < 40; i++) {
            synchronized (lock){
                if(isLetter){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName()+":"+index);
                index = index % 4 + 1;
                isLetter = !isLetter;
                lock.notifyAll();
            }
        }
    }

    public static void main(String[] args) {
        TwoThreadPrint10_abcd1234 twoThreadPrint10_abcd1234 = new TwoThreadPrint10_abcd1234();
        new Thread(() -> twoThreadPrint10_abcd1234.printLetter(),"thread1").start();
        new Thread(() -> twoThreadPrint10_abcd1234.printNum(),"thread2").start();
    }
}
